package com.ly.datacenter.jdbc.util;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import com.ly.datacenter.jdbc.mapping.SQLTypeMapCRUDType;
import com.ly.datacenter.jdbc.mapping.SQLTypeMapJavaType;

/**
 * function:
 * @author watson  email: watson.wu@hotmail.com
 * @date 2009-9-2
 */
@SuppressWarnings("unchecked")
public class SQLTypeParserUtil {
	
	static {
		
	}
	
	public static final int TYPE_UNKNOWN = Integer.MIN_VALUE;
	
	public static final SQLTypeMapJavaType[] MAP_2_SIMPLE_EDIT = {
		new SQLTypeMapJavaType("BIT", Types.BIT, int.class),
		new SQLTypeMapJavaType("TINYINT", 6, int.class),
		new SQLTypeMapJavaType("SMALLINT", 5, int.class),
		new SQLTypeMapJavaType("INTEGER", 4, int.class),
		new SQLTypeMapJavaType("BIGINT", -5, int.class),
		new SQLTypeMapJavaType("FLOAT", 6, int.class),
		new SQLTypeMapJavaType("REAL", 7, int.class),
		new SQLTypeMapJavaType("DOUBLE", 8, int.class),
		new SQLTypeMapJavaType("NUMERIC", 2, int.class),
		new SQLTypeMapJavaType("CHAR", 1, String.class),
		new SQLTypeMapJavaType("VARCHAR", 12, String.class),
		new SQLTypeMapJavaType("LONGVARCHAR", -1, String.class),
		new SQLTypeMapJavaType("DATE", 91, String.class),
		new SQLTypeMapJavaType("TIME", 92, String.class),
		new SQLTypeMapJavaType("TIMESTAMP", 93, String.class),
		new SQLTypeMapJavaType("BINARY", -2, String.class),
		new SQLTypeMapJavaType("VARBINARY", -3, String.class),
		new SQLTypeMapJavaType("LONGVARBINARY", -4, String.class),
		new SQLTypeMapJavaType("NULL", 0, null)
	};
	
	public static final SQLTypeMapCRUDType[] MAP_2_RICH_EDIT = {
		new SQLTypeMapCRUDType("BIT", -7, "option"),
		new SQLTypeMapCRUDType("TINYINT", 6, "num"),
		new SQLTypeMapCRUDType("SMALLINT", 5, "num"),
		new SQLTypeMapCRUDType("INTEGER", 4, "num"),
		new SQLTypeMapCRUDType("BIGINT", -5, "num"),
		new SQLTypeMapCRUDType("FLOAT", 6, "num"),
		new SQLTypeMapCRUDType("REAL", 7, "num"),
		new SQLTypeMapCRUDType("DOUBLE", 8, "num"),
		new SQLTypeMapCRUDType("NUMERIC", 2, "num"),
		new SQLTypeMapCRUDType("CHAR", 1, "string"),
		new SQLTypeMapCRUDType("VARCHAR", 12, "string"),
		new SQLTypeMapCRUDType("LONGVARCHAR", -1, "text"),
		new SQLTypeMapCRUDType("DATE", 91, "date"),
		new SQLTypeMapCRUDType("TIME", 92, "date"),
		new SQLTypeMapCRUDType("TIMESTAMP", 93, "date"),
		new SQLTypeMapCRUDType("BINARY", -2, "string"),
		new SQLTypeMapCRUDType("VARBINARY", -3, "string"),
		new SQLTypeMapCRUDType("LONGVARBINARY", -4, "string"),
		new SQLTypeMapCRUDType("NULL", 0, null)
	};
	
	public static final SQLTypeMapJavaType[] MAP_2_JAVA = {
		new SQLTypeMapJavaType("BIT", -7, boolean.class),
		new SQLTypeMapJavaType("TINYINT", 6, byte.class),
		new SQLTypeMapJavaType("SMALLINT", 5, short.class),
		new SQLTypeMapJavaType("INTEGER", 4, int.class),
		new SQLTypeMapJavaType("BIGINT", -5, long.class),
		new SQLTypeMapJavaType("FLOAT", 6, float.class),
		new SQLTypeMapJavaType("REAL", 7, float.class),
		new SQLTypeMapJavaType("DOUBLE", 8, double.class),
		new SQLTypeMapJavaType("NUMERIC", 2, BigDecimal.class),
		new SQLTypeMapJavaType("CHAR", 1, String.class),
		new SQLTypeMapJavaType("VARCHAR", 12, String.class),
		new SQLTypeMapJavaType("LONGVARCHAR", -1, String.class),
		new SQLTypeMapJavaType("DATE", 91, Date.class),
		new SQLTypeMapJavaType("TIME", 92, Time.class),
		new SQLTypeMapJavaType("TIMESTAMP", 93, Timestamp.class),
		new SQLTypeMapJavaType("BINARY", -2, byte[].class),
		new SQLTypeMapJavaType("VARBINARY", -3, byte[].class),
		new SQLTypeMapJavaType("LONGVARBINARY", -4, byte[].class),
		new SQLTypeMapJavaType("NULL", 0, null)
//		new DatabaseColumnTypeMapJavaType("", , )
	};

	public static Class map2JavaType(String sqlType) {
		if(null==sqlType) return null;
		for(SQLTypeMapJavaType e : MAP_2_JAVA) {
			if(e.getType().equalsIgnoreCase(sqlType)) {
				return e.getClazz();
			}
		}
		return null;
	}

	public static Class map2JavaType(int sqlType) {
		for(SQLTypeMapJavaType e : MAP_2_JAVA) {
			if(e.getValue() == sqlType) {
				return e.getClazz();
			}
		}
		return null;
	}

	public static String map2CRUDType(String sqlType) {
		for(SQLTypeMapCRUDType e : MAP_2_RICH_EDIT) {
			if(e.getColumnTypeName().equalsIgnoreCase(sqlType)) {
				return e.getType();
			}
		}
		return null;
	}
	
	public static String map2SQLType(String crudType) {
		for(SQLTypeMapCRUDType e : MAP_2_RICH_EDIT) {
			if(e.getType().equalsIgnoreCase(crudType))
				return e.getColumnTypeName(); 
		}
		return null;
	}

	public static int getSqlTypeValue(String sqlType) {
		for(SQLTypeMapJavaType e : MAP_2_JAVA) {
			if(e.getType().equalsIgnoreCase(sqlType)) {
				return e.getValue();
			}
		}
		return 0;
	}
}